library(haven)
library(car)
library(stargazer)
library(tidyverse)
 

#setwd("~/Dropbox/Project/switching on and off")


# Complete NAES data and full questionnaire are available at the Annenberg Public Policy Center's website. 

 naes <- read_csv("NAESpanel.csv")
 
 
 ## Sociotropic perceptions of the economy 
 
 naes$socio_5 <- car::recode(naes$ECO1_5, '-1=NA; 1=5; 2=4;3=2;4=1;5=3') 
 naes$socio_2 <- car::recode(naes$ECO1_2, '-1=NA; 1=5; 2=4;3=2;4=1;5=3') 
 naes$socio_diff_w5_w2 <- naes$socio_5 - naes$socio_2
 
 ## Covariates (from Wave 1)
 
 naes$dem1rep0 <- car::recode(naes$PARTY7_UPDATED_1, '1=0; 2=0; 3=0; 5=1; 6=1; 7=1; 4=NA')
 naes$ind <- car::recode(naes$partyid7_1, '-1=NA; 1=0; 2=0; 3=0; 4=1; 5=0; 6=0; 7=0')
 naes$male_w1 <- car::recode(naes$PPGENDER_1, '1=1; 2=0')
 naes$white_w1 <- car::recode(naes$PPETHM_1, '1=1; 2=0; 3=0; 4=0; 5=0')
 
 

 ## Dependent Variable = Different in the number of TV counts from wave 5 - wave 2 
 
 ## wave 2 
 
 naes$nightline_2 <- ifelse(naes$ME2A_2_2==1, 1, 0)
 naes$abcworldnews_2 <- ifelse(naes$ME2A_3_2==1, 1, 0)
 naes$todayshow_2 <- ifelse(naes$ME2A_4_2==1, 1, 0)
 naes$nbcnightlynews_2 <- ifelse(naes$ME2A_5_2==1, 1, 0)
 naes$lehrer_2  <- ifelse(naes$ME2A_6_2==1, 1, 0)
 naes$betnews_2  <- ifelse(naes$ME2A_7_2==1, 1, 0)
 naes$foxnews_2  <- ifelse(naes$ME2A_8_2==1, 1, 0)
 naes$goodmornamer_2 <- ifelse(naes$ME2A_9_2==1, 1, 0)
 naes$cbseveningnews_2 <-  ifelse(naes$ME2A_10_2==1, 1, 0)
 naes$cbsmorningnews_2 <-  ifelse(naes$ME2A_11_2==1, 1, 0)
 naes$cnnheadline_2 <- ifelse(naes$ME2A_12_2==1, 1, 0)
 naes$leno_2 <- ifelse(naes$ME2A_13_2==1, 1, 0) 
 naes$amerthismorn_2 <-  ifelse(naes$ME2A_14_2==1, 1,0) 
 naes$dailyshow_2 <-   ifelse(naes$ME2A_15_2==1, 1, 0) 
 
 naes$sixtymins_2 <-   ifelse(naes$ME2B_1_2==1, 1, 0) 
 naes$letterman_2 <-   ifelse(naes$ME2B_2_2==1, 1, 0) 
 naes$oreillyfactor_2 <-   ifelse(naes$ME2B_3_2==1, 1, 0) 
 naes$earlyshow_2 <-  ifelse(naes$ME2B_4_2==1, 1, 0) 
 naes$ellen_2 <-   ifelse(naes$ME2B_5_2==1, 1, 0) 
 naes$facenation_2 <-   ifelse(naes$ME2B_6_2==1, 1, 0) 
 naes$foxandfriends_2 <-   ifelse(naes$ME2B_7_2==1, 1, 0) 
 naes$frontline_2 <-   ifelse(naes$ME2B_9_2==1, 1, 0) 
 naes$hannitycolmes_2 <-   ifelse(naes$ME2B_10_2==1, 1, 0) 
 naes$hannityamer_2 <-  ifelse(naes$ME2B_11_2==1, 1, 0) 
 naes$hardball_2 <-  ifelse(naes$ME2B_12_2==1, 1, 0) 
 naes$heartland_2 <-  ifelse(naes$ME2B_13_2==1 , 1, 0) 
 naes$latewolfblitzer_2 <-   ifelse(naes$ME2B_15_2==1, 1, 0) 
 
 naes$meetpress_2 <-  ifelse(naes$ME4A_1_2==1, 1, 0) 
 naes$msnbclive_2 <-  ifelse(naes$ME4A_2_2==1, 1, 0) 
 naes$outinopen_2 <-   ifelse(naes$ME4A_4_2==1, 1, 0) 
 naes$oprah_2 <-   ifelse(naes$ME4A_5_2==1, 1, 0) 
 naes$situationroom_2 <-   ifelse(naes$ME4A_7_2==1, 1, 0) 
 naes$brithume_2 <-   ifelse(naes$ME4A_8_2==1, 1, 0) 
 naes$larryking_2 <-   ifelse(naes$ME4A_9_2==1, 1, 0) 
 naes$cbssundaymorn_2 <-   ifelse(naes$ME4A_10_2==1, 1, 0) 
 naes$beltwayboys_2 <-  ifelse(naes$ME4A_11_2==1, 1, 0) 
 naes$bigstory_2 <-  ifelse(naes$ME4A_12_2==1 , 1, 0) 
 naes$twentytwenty_2 <-   ifelse(naes$ME4A_13_2==1, 1, 0) 
 naes$foxshepardsmith_2 <-   ifelse(naes$ME4A_14_2==1, 1, 0) 
 
 naes$georgestephan_2 <-  ifelse(naes$ME4B_2_2==1, 1, 0) 
 naes$theview_2 <-  ifelse(naes$ME4B_3_2==1, 1, 0) 
 naes$loudobbs_2 <-  ifelse(naes$ME4B_5_2==1, 1, 0) 
 naes$colbert_2 <- ifelse(naes$ME4B_6_2==1, 1, 0) 
 naes$andersoncooper_2 <-  ifelse(naes$ME4B_7_2==1, 1, 0) 
 naes$geraldo_2 <- ifelse(naes$ME4B_8_2==1, 1, 0) 
 naes$olbermann_2 <- ifelse(naes$ME4B_9_2==1, 1, 0) 
 naes$datelinenbc_2 <- ifelse(naes$ME4B_10_2==1, 1, 0) 
 naes$studiobshepsmith_2 <- ifelse(naes$ME4B_12_2==1, 1, 0) 
 naes$reliablesources_2 <- ifelse(naes$ME4B_13_2==1, 1, 0) 
 naes$neilcavuto_2 <-  ifelse(naes$ME4B_14_2==1, 1, 0) 
 naes$mclaughlingroup_2 <-  ifelse(naes$ME4B_15_2==1, 1, 0) 
 
 naes$tvcount_liberal_2 <- naes$nightline_2 + naes$betnews_2 + naes$goodmornamer_2 +  naes$dailyshow_2 +
         naes$hardball_2  + naes$latewolfblitzer_2 + naes$msnbclive_2 + naes$outinopen_2  +
         naes$situationroom_2  + naes$georgestephan_2 +  naes$theview_2 + naes$colbert_2 +
         naes$andersoncooper_2 + naes$olbermann_2 
 
 naes$tvcount_neutral_2 <-  naes$abcworldnews_2 +  
         naes$todayshow_2 + naes$nbcnightlynews_2 + naes$lehrer_2 + naes$cbseveningnews_2 +
         naes$cbsmorningnews_2 + naes$cnnheadline_2 + naes$leno_2 +
         naes$amerthismorn_2 + naes$sixtymins_2 + naes$letterman_2 + 
         naes$earlyshow_2 + naes$ellen_2 + naes$facenation_2 + naes$frontline_2 + naes$meetpress_2 + naes$oprah_2 +
         naes$larryking_2 + naes$cbssundaymorn_2 + naes$twentytwenty_2 +
         naes$loudobbs_2 + naes$datelinenbc_2 + naes$reliablesources_2 + naes$mclaughlingroup_2
 
 naes$tvcount_conserv_2 <-        naes$foxnews_2 + 
         naes$oreillyfactor_2  +
         naes$foxandfriends_2 +
         naes$hannitycolmes_2 +
         naes$hannityamer_2  +
         naes$heartland_2 +
         naes$brithume_2 +
         naes$beltwayboys_2 +
         naes$bigstory_2 +
         naes$foxshepardsmith_2 +
         naes$geraldo_2 +
         naes$studiobshepsmith_2 +
         naes$neilcavuto_2
 
 naes$tvcount_2 = naes$tvcount_liberal_2 +  naes$tvcount_neutral_2 +  naes$tvcount_conserv_2
 
 
 #*WAVE 5 
 
 naes$outinopen_2 <-   ifelse(naes$ME4A_4_2==1, 1, 0) 
 naes$nightline_5 <- ifelse(naes$ME2a02_5==1, 1, 0)
 naes$abcworldnews_5<- ifelse(naes$ME2a03_5==1, 1, 0)
 naes$todayshow_5<- ifelse(naes$ME2a04_5==1, 1, 0)
 naes$nbcnightlynews_5<- ifelse(naes$ME2a05_5==1, 1, 0)
 naes$lehrer_5<- ifelse(naes$ME2a06_5==1, 1, 0)
 naes$betnews_5<- ifelse(naes$ME2a07_5==1, 1, 0)
 naes$foxnews_5<- ifelse(naes$ME2a08_5==1, 1, 0)
 naes$goodmornamer_5<- ifelse(naes$ ME2a09_5==1, 1, 0)
 naes$cbseveningnews_5<- ifelse(naes$ME2a10_5==1, 1, 0)
 naes$cbsmorningnews_5<- ifelse(naes$ME2a11_5==1, 1, 0)
 naes$cnnheadline_5<- ifelse(naes$ME2a12_5==1, 1, 0)
 naes$leno_5<- ifelse(naes$ME2a13_5==1, 1, 0)
 naes$amerthismorn_5<- ifelse(naes$ ME2a14_5==1, 1, 0)
 naes$dailyshow_5<- ifelse(naes$ME2a15_5==1, 1, 0)
 naes$sixtymins_5<- ifelse(naes$ME2b01_5==1, 1, 0)
 naes$letterman_5<- ifelse(naes$ME2b02_5==1, 1, 0)
 naes$oreillyfactor_5<- ifelse(naes$ME2b03_5==1, 1, 0)
 naes$earlyshow_5<- ifelse(naes$ME2b04_5==1, 1, 0)
 naes$ellen_5<- ifelse(naes$ME2b05_5==1, 1, 0)
 naes$facenation_5<- ifelse(naes$ME2b06_5==1, 1, 0)
 naes$foxandfriends_5<- ifelse(naes$ME2b07_5==1, 1, 0)
 naes$frontline_5<- ifelse(naes$ME2b09_5==1, 1, 0)
 naes$hannitycolmes_5<- ifelse(naes$ME2b10_5==1, 1, 0)
 naes$hannityamer_5<- ifelse(naes$ME2b11_5==1, 1, 0)
 naes$hardball_5<- ifelse(naes$ME2b12_5==1, 1, 0)
 naes$latewolfblitzer_5<- ifelse(naes$ ME2b14_5==1, 1, 0)
 naes$meetpress_5<- ifelse(naes$ME4A01_5==1, 1, 0)
 naes$msnbclive_5<- ifelse(naes$ME4A02_5==1, 1, 0)
 naes$outinopen_5<- ifelse(naes$ ME4A04_5==1, 1, 0)
 naes$oprah_5<- ifelse(naes$ME4A05_5==1, 1, 0)
 naes$situationroom_5<- ifelse(naes$ME4A07_5==1, 1, 0)
 naes$brithume_5<- ifelse(naes$ME4A08_5==1, 1, 0)
 naes$larryking_5<- ifelse(naes$ME4A09_5==1, 1, 0)
 naes$cbssundaymorn_5<- ifelse(naes$ME4A10_5==1, 1, 0)
 naes$beltwayboys_5<- ifelse(naes$ME4A11_5==1, 1, 0)
 naes$twentytwenty_5<- ifelse(naes$ME4A12_5==1, 1, 0)
 naes$foxshepardsmith_5<- ifelse(naes$ME4A13_5==1, 1, 0)
 naes$georgestephan_5<- ifelse(naes$ME4B02_5==1, 1, 0)
 naes$theview_5<- ifelse(naes$ME4B03_5==1, 1, 0)
 naes$loudobbs_5<- ifelse(naes$ME4B05_5==1, 1, 0)
 naes$colbert_5<- ifelse(naes$ME4B06_5==1, 1, 0)
 naes$andersoncooper_5<- ifelse(naes$ME4B07_5==1, 1, 0)
 naes$geraldo_5<- ifelse(naes$ME4B08_5==1, 1, 0)
 naes$olbermann_5<- ifelse(naes$ME4B09_5==1, 1, 0)
 naes$datelinenbc_5<- ifelse(naes$ME4B10_5==1, 1, 0)
 naes$studiobshepsmith_5<- ifelse(naes$ME4B12_5==1, 1, 0)
 naes$reliablesources_5<- ifelse(naes$ME4B13_5==1, 1, 0)
 naes$neilcavuto_5<- ifelse(naes$ME4B14_5==1, 1, 0)
 naes$mclaughlingroup_5<- ifelse(naes$ME4B15_5==1, 1, 0)
 naes$americaselection_5  <- ifelse(naes$ME2a16_5==1, 1, 0)
 naes$morningjoe_5 <-   ifelse(naes$ME2b15_5==1, 1,0)
 naes$rachelmaddow_5 <-  ifelse(naes$ME2b16_5==1,1,0)
 naes$campbell_5   <- ifelse(naes$ME4A14_5==1,1,0) 
 
 
 naes$tvcount_liberal_5 = naes$andersoncooper_5 + naes$olbermann_5 + naes$hardball_5 + 
         naes$cnnheadline_5 + naes$nightline_5 + naes$situationroom_5 +
         naes$dailyshow_5 + naes$goodmornamer_5 + naes$georgestephan_5 +
         naes$theview_5 + naes$colbert_5 + naes$latewolfblitzer_5 + naes$msnbclive_5 +  
         naes$outinopen_5 + naes$betnews_5 + naes$morningjoe_5 + naes$rachelmaddow_5
 
 naes$tvcount_neutral_5 = naes$abcworldnews_5 + naes$cbseveningnews_5 + naes$loudobbs_5 +  naes$nbcnightlynews_5 + 
         naes$meetpress_5 + naes$todayshow_5 + naes$lehrer_5 + naes$larryking_5 + naes$sixtymins_5 + 
         naes$facenation_5 +  naes$reliablesources_5 + naes$earlyshow_5 + naes$frontline_5 + naes$cbssundaymorn_5  + naes$twentytwenty_5 + 
         naes$datelinenbc_5 +naes$mclaughlingroup_5 + naes$cbsmorningnews_5 + naes$amerthismorn_5 + 
         naes$leno_5 + naes$americaselection_5 + naes$letterman_5 + naes$ellen_5 + naes$oprah_5 + naes$campbell_5
 
 naes$tvcount_conserv_5 = naes$foxnews_5 +  naes$foxshepardsmith_5 + naes$hannitycolmes_5 + naes$oreillyfactor_5 + naes$brithume_5 + naes$hannityamer_5 + 
         naes$beltwayboys_5 + naes$studiobshepsmith_5 + naes$geraldo_5 + naes$neilcavuto_5 +  naes$foxandfriends_5
 
 naes$tvcount_5 = naes$tvcount_liberal_5 + naes$tvcount_neutral_5 + naes$tvcount_conserv_5
 
 naes$tvcount_diff_w5_w2 <- naes$tvcount_5 -  naes$tvcount_2 
 
 ## Sociotropic perceptions of the economy 
 
 naes$socio_5 <- car::recode(naes$ECO1_5, '-1=NA; 1=5; 2=4;3=2;4=1;5=3') 
 naes$socio_2 <- car::recode(naes$ECO1_2, '-1=NA; 1=5; 2=4;3=2;4=1;5=3') 
 naes$socio_diff_w5_w2 <- naes$socio_5 - naes$socio_2
 
 ## Covariates (from Wave 1)
 
 naes$dem1rep0 <- car::recode(naes$PARTY7_UPDATED_1, '1=0; 2=0; 3=0; 5=1; 6=1; 7=1; 4=NA')
 naes$ind <- car::recode(naes$partyid7_1, '-1=NA; 1=0; 2=0; 3=0; 4=1; 5=0; 6=0; 7=0')
 naes$male_w1 <- car::recode(naes$PPGENDER_1, '1=1; 2=0')
 naes$white_w1 <- car::recode(naes$PPETHM_1, '1=1; 2=0; 3=0; 4=0; 5=0')
 
 
 
 ## regression results 

 
 m1 <- lm(tvcount_diff_w5_w2 ~ dem1rep0, data=naes,  weights= weight_5) 
 m2 <- lm(tvcount_diff_w5_w2 ~ dem1rep0 + PPAGE_1 + male_w1 + white_w1 + PPEDUCAT_1 + PPINCIMP_1 + PPNET_1, data=naes, weights= weight_5) 
 md <- lm(socio_diff_w5_w2  ~ tvcount_diff_w5_w2, data=naes[naes$dem1rep0==1,], weights= weight_5) 
 md1 <- lm(socio_diff_w5_w2  ~ tvcount_diff_w5_w2 +  dem1rep0 + PPAGE_1 + male_w1 + white_w1 + PPEDUCAT_1 + PPINCIMP_1 + PPNET_1, data=naes[naes$dem1rep0==1,], weights= weight_5) 
 mr <- lm(socio_diff_w5_w2  ~ tvcount_diff_w5_w2, data=naes[naes$dem1rep0==0,], weights= weight_5) 
 mr1 <- lm(socio_diff_w5_w2  ~ tvcount_diff_w5_w2 +  dem1rep0 + PPAGE_1 + male_w1 + white_w1 + PPEDUCAT_1 + PPINCIMP_1 + PPNET_1, data=naes[naes$dem1rep0==0,], weights= weight_5) 
 
 
 
 ## Table 4 / Table E8 
 
 library(stargazer)
 table <- capture.output({stargazer(m1,m2,md,md1,mr,mr1, 
                                    dep.var.labels=c('Change in # TV #','Change in Economic Perceptions', 'Change in Economic Perceptions'),
                                    omit.stat=c("adj.rsq","LL","ser","f"),
                                    column.labels = c('All','Dem Only','Rep Only'),
                                    column.separate = c(2,2,2),
                                    no.space=TRUE,
                                    star.char = c("*", "**", "***"), 
                                    star.cutoffs = c(.05,.01,.001),
                                    notes = c("* p<0.05; ** p<0.01; *** p<0.001"), 
                                    notes.append = F,
                                    notes.align="l",
                                    add.lines = list(c("Weighted?", "Yes", "Yes","Yes","Yes","Yes","Yes","Yes","Yes"),
                                                     c("Covariates Included?", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes")),
                                    digits=3,
                                    align = TRUE) 
 })
 table <- gsub("\\begin{tabular}","\\resizebox{1\\textwidth}{!}{\\begin{tabular}", table,fixed=T)
 table <- gsub("\\end{tabular}","\\end{tabular}}", table,fixed=T)
 cat(table)  
 
 ## Table E9 
 
 mi <- lm(tvcount_diff_w5_w2 ~ ind, data=naes,  weights= weight_5) 
 mi2 <- lm(tvcount_diff_w5_w2 ~ ind +  PPAGE_1 + male_w1 + white_w1 + PPEDUCAT_1 + PPINCIMP_1 + PPNET_1 , data=naes,  weights= weight_5) 
 mi3 <- lm(socio_diff_w5_w2  ~ tvcount_diff_w5_w2, data=naes[naes$ind==1,], weights= weight_5) 
 mi4 <- lm(socio_diff_w5_w2  ~ tvcount_diff_w5_w2 +  PPAGE_1 + male_w1 + white_w1 + PPEDUCAT_1 + PPINCIMP_1 + PPNET_1, data=naes[naes$ind==1,], weights= weight_5) 
 
 
 table <- capture.output({stargazer(mi,mi2,mi3,mi4, 
                                    dep.var.labels=c('Change in # TV #','Change in Economic Perceptions'),
                                    omit.stat=c("adj.rsq","LL","ser","f"),
                                    column.labels = c('All','Independents Only'),
                                    column.separate = c(2,2,2),
                                    no.space=TRUE,
                                    star.char = c("*", "**", "***"), 
                                    notes = c("* p<0.05; ** p<0.01; *** p<0.001"), 
                                    notes.append = F,
                                    notes.align="l",
                                    add.lines = list(c("Weighted?", "Yes", "Yes","Yes","Yes"),
                                                     c("Covariates Included?", "No", "Yes", "No", "Yes")),
                                    digits=3,
                                    align = TRUE) 
 })
 table <- gsub("\\begin{tabular}","\\resizebox{1\\textwidth}{!}{\\begin{tabular}", table,fixed=T)
 table <- gsub("\\end{tabular}","\\end{tabular}}", table,fixed=T)
 cat(table)  
 
 


 